/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DisponibilidadeProfessoresDisciplinaIFrame.java
 *
 * Created on 31/05/2012, 21:53:22
 */

package br.com.fatecpg.operon.frames;

import br.com.fatecpg.operon.Conexao;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
//import javax.swing.JComboBox;



public class DisponibilidadeProfessoresDisciplinaIFrame extends javax.swing.JInternalFrame {
  public DefaultListModel listaDisp = new DefaultListModel();
  public   DefaultListModel listaMin = new DefaultListModel();
  
   public String codDisc="",campo="",digitou="", nome="",titulo="",disc="",titulocurso="";
   private String sqldisc="SELECT c.TITULO, d.TITULO, d.DISCIPLINA_ID FROM  CURSOS c, DISCIPLINAS d WHERE d.CURSO_ID = c.CURSO_ID ";
   private String sqlcurso = "SELECT TITULO FROM  CURSOS ";//c, DISCIPLINAS d WHERE d.CURSO_ID = c.CURSO_ID ";   
    private String sqldisp = "SELECT d.TITULO"
            + " FROM  DISCIPLINAS d, DISP_DISCIPLINAS dd "+ 
            " WHERE dd.DISCIPLINA_ID = d.DISCIPLINA_ID ";
   //" LIKE %" + digitou + "%"; 
    private String sql="SELECT PROF_ID, NOME FROM PROFESSORES  WHERE " + campo +
                " LIKE %" + digitou + "%"; 
   private String cod = "";
    
  public DisponibilidadeProfessoresDisciplinaIFrame() {
        initComponents();
        
    }



        @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtnmprofessor = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstMin = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        lstDisp = new javax.swing.JList();
        btDisp = new javax.swing.JButton();
        btnMin = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        btnSave = new javax.swing.JButton();
        btnConsulta = new javax.swing.JButton();
        btnLimpa = new javax.swing.JButton();
        txtcod = new javax.swing.JTextField();
        cmbCurso = new javax.swing.JComboBox();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Disponibilidade Professores para Disciplina");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jLabel1.setText("Código Professor");

        jLabel2.setText("Nome");

        txtnmprofessor.setToolTipText("Digite o nome ou parte dele ");

        lstMin.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstMin.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstMinValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(lstMin);

        lstDisp.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane3.setViewportView(lstDisp);

        btDisp.setText(">");
        btDisp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDispActionPerformed(evt);
            }
        });

        btnMin.setText("<");
        btnMin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMinActionPerformed(evt);
            }
        });

        jLabel3.setText("Disciplinas Ministradas");

        jLabel4.setText("Disciplinas Disponíveis");

        jLabel5.setText("Curso");

        btnSave.setText("SALVAR");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnConsulta.setText("Consultar");
        btnConsulta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultaActionPerformed(evt);
            }
        });

        btnLimpa.setText("Limpar");
        btnLimpa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpaActionPerformed(evt);
            }
        });

        txtcod.setEditable(false);
        txtcod.setToolTipText("consulte pelo nome");

        cmbCurso.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cursos" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(34, 34, 34)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(btDisp)
                                    .addComponent(btnMin))
                                .addGap(40, 40, 40)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmbCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(146, 146, 146))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addGap(18, 18, 18)
                                        .addComponent(txtnmprofessor, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel1)
                                        .addGap(18, 18, 18)
                                        .addComponent(txtcod, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(btnConsulta)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addComponent(btnLimpa))))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(154, 154, 154)
                        .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtcod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnConsulta)
                        .addGap(7, 7, 7)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtnmprofessor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnLimpa))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(cmbCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(btDisp)
                        .addGap(18, 18, 18)
                        .addComponent(btnMin)))
                .addGap(18, 18, 18)
                .addComponent(btnSave)
                .addContainerGap(42, Short.MAX_VALUE))
        );

        jLabel3.getAccessibleContext().setAccessibleName("Matérias ministradas");

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-451)/2, (screenSize.height-369)/2, 451, 369);
    }// </editor-fold>//GEN-END:initComponents

    private void btDispActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDispActionPerformed
        AddElementos();
  
    }//GEN-LAST:event_btDispActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        
        try {
            ConsultaCursos();
            listaMin.addElement(ConsultaDisc());
             listaMin.addElement(ConsultaDispon());
        } catch (SQLException ex) {
            Logger.getLogger(DisponibilidadeProfessoresDisciplinaIFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
      
        
       
      
    }//GEN-LAST:event_formInternalFrameOpened
    public void Consulta() throws SQLException{
        ResultSet resu = Conexao.con.createStatement().executeQuery(sql);
          while(resu.next()){
          cod =Integer.toString( resu.getInt("PROF_ID"));
          nome = resu.getString("NOME"); 
                    
      }
    }
    
    
public List<String> ConsultaDisc() throws SQLException{
       List<String> dados = new ArrayList<String>();
         ResultSet resu = Conexao.con.createStatement().executeQuery(sqldisc);
          while(resu.next()){
         dados.add(resu.getString("d.TITULO"));
                  /*CREATE TABLE CURSOS(
   CURSO_ID                integer           primary key
   , SIGLA                 varchar2(20)      unique
   , TITULO                varchar2(150)
   , PERIODOS              integer
   , PERFIL_PROFISSIONAL   varchar2(512)
)
*/  
      }return dados;
    } 
     
     public  void ConsultaCursos() throws SQLException{
        ResultSet resu = Conexao.con.createStatement().executeQuery(sqlcurso);
        DefaultComboBoxModel model = (DefaultComboBoxModel) cmbCurso.getModel();
        model.removeAllElements();       
        
        while(resu.next()){
          model.addElement(resu.getString("TITULO"));
          
       
   } 
     }
     public List<String> ConsultaDispon() throws SQLException{
        ResultSet resu = Conexao.con.createStatement().executeQuery(sqldisp);
       List<String> disponi = new ArrayList<String>();
        while(resu.next()){
          disponi.add(resu.getString("d.TITULO")); 
                 
      }return disponi;
    }
  
     public void  AddElementos(){
            //adicionar linha
        DefaultListModel model =  new DefaultListModel();//) List2.getModel();
       
         
     //  DefaultListModel model1 =  (DefaultListModel) List1.getModel();
         JList lista2 = lstDisp;
         JList lista1 = lstMin;
       
        String item = (String) lista1.getSelectedValue();
      
                  
   //     indice = lista2.countComponents();
      ///  JOptionPane.showMessageDialog(null, indice);
     /*/   DefaultListModel model2 = new DefaultListModel();//) lista2.getModel();//) List2.getModel();
        model2.addElement(valores);        
        auxiliar.setModel(model2);;*/
        
    //    model.addElement(valores); 
        model.addElement(lista1.getSelectedValue());  
        lista2.setModel(model);
      
     }
     
       public void  RemoveElementos(){
            //adicionar linha
        DefaultListModel model =  new DefaultListModel();//) List2.getModel();
       
         
     //  DefaultListModel model1 =  (DefaultListModel) List1.getModel();
         JList lista2 = lstDisp;
         JList lista1 = lstMin;
       
        String item = (String) lista1.getSelectedValue();
      
                  
   //     indice = lista2.countComponents();
      ///  JOptionPane.showMessageDialog(null, indice);
     /*/   DefaultListModel model2 = new DefaultListModel();//) lista2.getModel();//) List2.getModel();
        model2.addElement(valores);        
        auxiliar.setModel(model2);;*/
        
    //    model.addElement(valores); 
        model.addElement(lista2.getSelectedValue());  
        lista1.setModel(model);
      
     }
     
    
    private void lstMinValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstMinValueChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_lstMinValueChanged

    private void btnLimpaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpaActionPerformed
        txtcod.setText("");
        txtnmprofessor.setText("");
    }//GEN-LAST:event_btnLimpaActionPerformed

    private void btnConsultaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultaActionPerformed
     if("".equals(txtnmprofessor.getText())){
           JOptionPane.showMessageDialog(null,"Digite o nome do professor ou parte dele");
     }else{
            try {
                Consulta();
            } catch (SQLException ex) {
                Logger.getLogger(DisponibilidadeProfessoresDisciplinaIFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            txtcod.setText(cod);
            txtnmprofessor.setText(nome);
         
     }
    }//GEN-LAST:event_btnConsultaActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
  ////*************************************************arrumar
        Integer DISP_DISC_ID=0;//auto incrementar
        // Integer PROF_ID = Integer.toString(txtcod.getText());
       // Integer DISCIPLINA_ID = codDisc ;
       String sql = "INSERT INTO DISP_DISCIPLINAS VALUES(1,1,1)";
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnMinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMinActionPerformed
        RemoveElementos();
    }//GEN-LAST:event_btnMinActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btDisp;
    private javax.swing.JButton btnConsulta;
    private javax.swing.JButton btnLimpa;
    private javax.swing.JButton btnMin;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cmbCurso;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList lstDisp;
    private javax.swing.JList lstMin;
    private javax.swing.JTextField txtcod;
    private javax.swing.JTextField txtnmprofessor;
    // End of variables declaration//GEN-END:variables

   

   
}
